#include <iostream>
#include <string>
#include <stack>

using namespace std;

class Solution {
public:
    int calculate(string s)
    {
        stack<int> st;
        char op = '+';
        int n = s.size(), i = 0;
        while (i < n)
        {
            if (s[i] == ' ') i++;
            else if (s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while (i < n && s[i] >= '0' && s[i] <= '9')
                {
                    tmp = tmp * 10 + (s[i] - '0');
                    i++;
                }

                if (op == '+')
                {
                    st.push(tmp);
                }
                else if (op == '-')
                {
                    st.push(-tmp);
                }
                else if (op == '*')
                {
                    st.top() *= tmp;
                }
                else if (op == '/')
                {
                    if (st.top())
                        st.top() /= tmp;
                }
            }
            else
            {
                op = s[i];
                i++;
            }
        }

        int ret = 0;
        while (st.size())
        {
            ret += st.top();
            st.pop();
        }

        return ret;
    }
};

int main()
{
    Solution s;
    string ss = "3 + 1*2 -1 + 3*3";
    s.calculate(ss);
    return 0;
}

